
package com.yimixian.app.rest.api;

import android.text.TextUtils;

import com.ta.utdid2.android.utils.StringUtils;
import com.yimixian.app.common.CacheKeys;
import com.yimixian.app.common.SharePreferenceKeys;
import com.yimixian.app.data.DataManager;
import com.yimixian.app.model.Address;
import com.yimixian.app.model.GoodsItem;
import com.yimixian.app.model.SearchKeyResult;
import com.yimixian.app.model.SortGoodsItem;
import com.yimixian.app.util.SharedPreferencesHelper;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import retrofit.Callback;
import retrofit.client.Response;

/**
 * Created by szy on 2016/1/20.
 */
public class SearchKeyAPI extends YmxBaseAPI<SearchKeyResult> {

    public static final String SEARCH_KEY = "/v5/search";
    public List<SortGoodsItem> goodsItems;
    /**
     * @param callback
     */
    public SearchKeyAPI(Callback<Response> callback) {
        super(callback);
    }

    @Override
    public String getRelativeURL() {
        return SEARCH_KEY;
    }

    public void getSearchKeyResult(String keyWord,int page) {
        Address address = SharedPreferencesHelper.getAddress(SharePreferenceKeys.CURRENT_ADDRESS);
        String storeIdStr = "";
        if (address != null) {
            storeIdStr = address.storeId;
        }
        String token = DataManager.getInstance().get(CacheKeys.TOKEN);
        int storeId = 0;
        if(!TextUtils.isEmpty(storeIdStr)) {
            storeId = Integer.parseInt(storeIdStr);
        }
        if(TextUtils.isEmpty(token)) {
            token = "";
        }
        mYmxRestService.getSearchKeyResult(storeId,token,keyWord,page,mCallback);
    }

    @Override
    public SearchKeyResult parseJson(JSONObject jsonObject) {
        SearchKeyResult searchKeyResult = new SearchKeyResult();
        searchKeyResult.hotGoods = new ArrayList<>();
        searchKeyResult.searchResults = new ArrayList<>();
        if(jsonObject != null) {
            searchKeyResult.isMore = jsonObject.optInt("is_more");
            searchKeyResult.nextPage = jsonObject.optInt("next_page");
            JSONArray searchResultArray = jsonObject.optJSONArray("search_result");
            if(searchResultArray != null) {
                for(int i=0;i<searchResultArray.length();i++) {
                    JSONObject searchResultObj = searchResultArray.optJSONObject(i);
                    if(searchResultObj != null) {
                        GoodsItem goodsItem = new GoodsItem();
                        goodsItem.id = searchResultObj.optInt("id");
                        goodsItem.name = searchResultObj.optString("name");
                        if (!StringUtils.isEmpty(searchResultObj.optString("img_ratio"))){
                            goodsItem.imgRatio = searchResultObj.optInt("img_ratio");
                        }
                        goodsItem.img = searchResultObj.optString("img");
                        goodsItem.promotionImg = searchResultObj
                            .optString("promotion_img");
                        goodsItem.descUrl = searchResultObj.optString("desc_url");
                        goodsItem.unitDesc = searchResultObj.optString("unit_desc");
                        goodsItem.saleStatus = searchResultObj.optString("sale_status");
                        goodsItem.unitPrice = searchResultObj.optString("unit_price");
                        goodsItem.stockCount = searchResultObj.optString("stock_count");
                        goodsItem.unitOldPrice = searchResultObj
                            .optString("unit_old_price");
                        goodsItem.shouldBuyCount = searchResultObj
                            .optInt("should_buy_count");
                        goodsItem.isBuyingGroup = searchResultObj
                            .optInt("is_buying_group");
                        goodsItem.buyingGroupCount = searchResultObj
                            .optInt("buying_group_count");
                        goodsItem.buyingGroupPrice = searchResultObj
                            .optInt("buying_group_price");
                        goodsItem.countZeroReason = searchResultObj
                            .optString("count_zero_reason");
                        goodsItem.start_time = searchResultObj.optLong("start_time");
                        searchKeyResult.searchResults.add(goodsItem);
                    }
                }
            }
            JSONArray hotGoodsArray = jsonObject.optJSONArray("hot_goods");
            if(hotGoodsArray != null) {
                for(int i=0;i<hotGoodsArray.length();i++) {
                    JSONObject searchResultObj = hotGoodsArray.optJSONObject(i);
                    if(searchResultObj != null) {
                        GoodsItem goodsItem = new GoodsItem();
                        goodsItem.id = searchResultObj.optInt("id");
                        goodsItem.name = searchResultObj.optString("name");
                        if (!StringUtils.isEmpty(searchResultObj.optString("img_ratio"))){
                            goodsItem.imgRatio = searchResultObj.optInt("img_ratio");
                        }
                        goodsItem.img = searchResultObj.optString("img");
                        goodsItem.promotionImg = searchResultObj
                            .optString("promotion_img");
                        goodsItem.descUrl = searchResultObj.optString("desc_url");
                        goodsItem.unitDesc = searchResultObj.optString("unit_desc");
                        goodsItem.saleStatus = searchResultObj.optString("sale_status");
                        goodsItem.unitPrice = searchResultObj.optString("unit_price");
                        goodsItem.stockCount = searchResultObj.optString("stock_count");
                        goodsItem.unitOldPrice = searchResultObj
                            .optString("unit_old_price");
                        goodsItem.shouldBuyCount = searchResultObj
                            .optInt("should_buy_count");
                        goodsItem.isBuyingGroup = searchResultObj
                            .optInt("is_buying_group");
                        goodsItem.buyingGroupCount = searchResultObj
                            .optInt("buying_group_count");
                        goodsItem.buyingGroupPrice = searchResultObj
                            .optInt("buying_group_price");
                        goodsItem.countZeroReason = searchResultObj
                            .optString("count_zero_reason");
                        goodsItem.start_time = searchResultObj.optLong("start_time");
                        searchKeyResult.hotGoods.add(goodsItem);
                    }
                }
            }
        }
        return searchKeyResult;
    }

    public List<SortGoodsItem> translateArray(List<GoodsItem> goodsItemList){
        goodsItems = new ArrayList<>();

        if (goodsItemList == null) {
            return goodsItems;
        }
        List<GoodsItem> items = goodsItemList;
        int size = items.size();
        //添加section后面的item
        for (int j = 0; j < size; j++){
            SortGoodsItem sortGoodsItem = new SortGoodsItem(SortGoodsItem.ITEM);
            if (2 * j <= size - 1) {
                sortGoodsItem.leftGoodsItem = items.get(2 * j);
                if (2 * j + 1 <= size - 1){
                    sortGoodsItem.RightGoodsItem = items.get(2 * j + 1);
                }
                goodsItems.add(sortGoodsItem);
            }

        }
        return goodsItems;
    }
}
